package Messages;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Logic.SendMail;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * this class implements the subscriber can extend lending db
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class MessageExtendLending {

	
	public String bookid,title,wanted;
	static int row1,temp;
	PreparedStatement p1,p2,p6;
	Date enddate;
	/**
	 * constractor of this class 
	 * @param client
	 */
	public MessageExtendLending() {
		super();
	
	}
	/**
	 * get bookid
	 * @return
	 */
	public String getBookid() {
		return bookid;
	}
	/**
	 * set book id
	 * @param bookid
	 */
	public void setBookid(String bookid) {
		this.bookid = bookid;
	}
	/**
	 * get book title
	 * @return
	 */
	public String getTitle() {
		return title;
	}
	/**
	 * set book title
	 * @param title
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	/**
	 * get the date
	 * @return
	 */
	public Date getDate() {
		return enddate;
	}
	/**
	 * set the date
	 * @param date
	 */
	public void setDate(Date date) {
		this.enddate = date;
	}
	/**
	 * the method update the return date of the wanted book in the db
	 * @param str
	 */
	public String extendlend(String[] str){
		
		try 
		{

			//System.out.println("extending lend");
			Statement stmt;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			ResultSet rs= stmt.executeQuery("SELECT * FROM db1.lending WHERE CopyId='"+str[1]+"' AND SubscriberId='"+str[2]+"'AND Lend='"+"yes"+"';");
				try {
				
						if (rs.next())//check if subscriber lended this book
							{
								Date d=rs.getDate(5);
								p1=conn.getConnection().prepareStatement("SELECT * FROM db1.reservation WHERE BookId='"+str[1]+"';");
								ResultSet rs1 =p1.executeQuery();
								if(!rs1.next()){//check if book not exist in reservation
								
									PreparedStatement p3 = conn.getConnection().prepareStatement("SELECT * FROM db1.books,db1.bookscopies WHERE bookscopies.CopyId = ? AND books.ID=bookscopies.BID;");//retrive wanted status
									p3.setString(1,str[1]);
									ResultSet rs2 = p3.executeQuery();  
									rs2.next();
									wanted=rs2.getString(10);
									java.util.Date today = new java.util.Date();
									java.sql.Date timestamp = new java.sql.Date(today.getTime());
									int day=timestamp.getDate();
									enddate=rs.getDate(5);
								//	System.out.println("today:"+timestamp);	
									//System.out.println("end date:"+enddate);
									
									if(!wanted.equals("Yes")){//check for book wanted status
										
										enddate=rs.getDate(5);
										//System.out.println("today:"+timestamp);	
										//System.out.println("end date:"+enddate);
										timestamp.setDate(day+7);//extend by 7 days
													
															
									if(timestamp.after(enddate)){
										//System.out.println("before");
										 PreparedStatement ps9 = conn.getConnection().prepareStatement("update db1.lending set EndDate = ?  where `CopyId` = '"+str[1]+"'");
										 ps9.setDate(1, timestamp);
										 ps9.executeUpdate();
										
										
										 return ("ExtendReturnDatecomplety");
									} 
										   else{
											   
												//System.out.println("not before");	
												return ("CantExtendDateTooShort");
										   }
										  
															 
								}
									else{
										//System.out.println("Cant extend the book is wanted");	
										return ("Cantextendthebookiswanted");
										
											}
								}	
									
									else{
										return ("BookAllreadyReserved");
											}
								
											
							}
								else 
							{
									return ("Bookisnotlended");							
							}
  
					}
						catch (SQLException e)
								{	
								e.printStackTrace();
								}
		}
  
		catch (Exception ex )
		{
	  ex.printStackTrace();
		}
		return ("fail");
		
		
	}
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

